using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Matrix.Notification.Channels;

namespace Matrix.Notification.Services
{
    /// <summary>
    /// 渠道配置服务接口
    /// </summary>
    public interface IChannelConfigurationService
    {
        /// <summary>
        /// 获取渠道配置
        /// </summary>
        /// <param name="channel">渠道类型</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>渠道配置</returns>
        Task<ChannelConfiguration?> GetConfigurationAsync(
            NotificationChannel channel,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取所有渠道配置
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>渠道配置列表</returns>
        Task<List<ChannelConfiguration>> GetAllConfigurationsAsync(
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 更新渠道配置
        /// </summary>
        /// <param name="channel">渠道类型</param>
        /// <param name="configuration">渠道配置</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>更新结果</returns>
        Task<ChannelConfigurationUpdateResult> UpdateConfigurationAsync(
            NotificationChannel channel,
            ChannelConfiguration configuration,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 启用/禁用渠道
        /// </summary>
        /// <param name="channel">渠道类型</param>
        /// <param name="enabled">是否启用</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>操作结果</returns>
        Task<bool> SetChannelEnabledAsync(
            NotificationChannel channel,
            bool enabled,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 测试渠道连接
        /// </summary>
        /// <param name="channel">渠道类型</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>测试结果</returns>
        Task<ChannelTestResult> TestChannelAsync(
            NotificationChannel channel,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取渠道统计信息
        /// </summary>
        /// <param name="channel">渠道类型</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>统计信息</returns>
        Task<ChannelStatistics> GetChannelStatisticsAsync(
            NotificationChannel channel,
            DateTime? startTime = null,
            DateTime? endTime = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 重置渠道配置为默认值
        /// </summary>
        /// <param name="channel">渠道类型</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>操作结果</returns>
        Task<bool> ResetToDefaultAsync(
            NotificationChannel channel,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 导出渠道配置
        /// </summary>
        /// <param name="channels">渠道列表（可选）</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>配置导出数据</returns>
        Task<ChannelConfigurationExport> ExportConfigurationsAsync(
            IEnumerable<NotificationChannel>? channels = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 导入渠道配置
        /// </summary>
        /// <param name="exportData">配置导出数据</param>
        /// <param name="overwriteExisting">是否覆盖现有配置</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>导入结果</returns>
        Task<ChannelConfigurationImportResult> ImportConfigurationsAsync(
            ChannelConfigurationExport exportData,
            bool overwriteExisting = false,
            CancellationToken cancellationToken = default);
    }

    /// <summary>
    /// 渠道配置
    /// </summary>
    public class ChannelConfiguration
    {
        /// <summary>
        /// 渠道类型
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 配置名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 配置描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 配置参数
        /// </summary>
        public Dictionary<string, object> Settings { get; set; } = new();

        /// <summary>
        /// 连接测试状态
        /// </summary>
        public ConnectionTestStatus TestStatus { get; set; }

        /// <summary>
        /// 最后测试时间
        /// </summary>
        public DateTime? LastTestTime { get; set; }

        /// <summary>
        /// 最后测试结果
        /// </summary>
        public string? LastTestResult { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime LastUpdatedTime { get; set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public int Version { get; set; }

        /// <summary>
        /// 配置标签
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 是否为默认配置
        /// </summary>
        public bool IsDefault { get; set; }
    }

    /// <summary>
    /// 渠道配置更新结果
    /// </summary>
    public class ChannelConfigurationUpdateResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 验证错误列表
        /// </summary>
        public List<ConfigurationValidationError> ValidationErrors { get; set; } = new();

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdatedTime { get; set; }

        /// <summary>
        /// 更新的配置
        /// </summary>
        public ChannelConfiguration? Configuration { get; set; }
    }

    /// <summary>
    /// 配置验证错误
    /// </summary>
    public class ConfigurationValidationError
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string Field { get; set; } = string.Empty;

        /// <summary>
        /// 错误信息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 错误代码
        /// </summary>
        public string ErrorCode { get; set; } = string.Empty;

        /// <summary>
        /// 严重级别
        /// </summary>
        public ValidationSeverity Severity { get; set; }
    }

    /// <summary>
    /// 验证严重级别
    /// </summary>
    public enum ValidationSeverity
    {
        /// <summary>
        /// 信息
        /// </summary>
        Info,

        /// <summary>
        /// 警告
        /// </summary>
        Warning,

        /// <summary>
        /// 错误
        /// </summary>
        Error,

        /// <summary>
        /// 严重错误
        /// </summary>
        Critical
    }

    /// <summary>
    /// 连接测试状态
    /// </summary>
    public enum ConnectionTestStatus
    {
        /// <summary>
        /// 未测试
        /// </summary>
        NotTested,

        /// <summary>
        /// 测试成功
        /// </summary>
        Success,

        /// <summary>
        /// 测试失败
        /// </summary>
        Failed,

        /// <summary>
        /// 测试中
        /// </summary>
        Testing
    }

    /// <summary>
    /// 渠道测试结果
    /// </summary>
    public class ChannelTestResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 测试时间
        /// </summary>
        public DateTime TestTime { get; set; }

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public long ResponseTimeMs { get; set; }

        /// <summary>
        /// 测试消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 详细信息
        /// </summary>
        public Dictionary<string, object> Details { get; set; } = new();

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 异常信息
        /// </summary>
        public Exception? Exception { get; set; }

        /// <summary>
        /// 测试类型
        /// </summary>
        public ChannelTestType TestType { get; set; }
    }

    /// <summary>
    /// 渠道测试类型
    /// </summary>
    public enum ChannelTestType
    {
        /// <summary>
        /// 连接测试
        /// </summary>
        Connection,

        /// <summary>
        /// 认证测试
        /// </summary>
        Authentication,

        /// <summary>
        /// 发送测试
        /// </summary>
        Send,

        /// <summary>
        /// 完整测试
        /// </summary>
        Full
    }

    /// <summary>
    /// 渠道统计信息
    /// </summary>
    public class ChannelStatistics
    {
        /// <summary>
        /// 渠道类型
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 统计时间范围
        /// </summary>
        public StatisticsPeriod Period { get; set; }

        /// <summary>
        /// 总发送数量
        /// </summary>
        public long TotalSent { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public long FailureCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalSent > 0 ? (double)SuccessCount / TotalSent : 0;

        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public double AverageResponseTimeMs { get; set; }

        /// <summary>
        /// 最大响应时间（毫秒）
        /// </summary>
        public long MaxResponseTimeMs { get; set; }

        /// <summary>
        /// 最小响应时间（毫秒）
        /// </summary>
        public long MinResponseTimeMs { get; set; }

        /// <summary>
        /// 按状态码分组的统计
        /// </summary>
        public Dictionary<string, long> StatusCodeBreakdown { get; set; } = new();

        /// <summary>
        /// 按错误类型分组的统计
        /// </summary>
        public Dictionary<string, long> ErrorBreakdown { get; set; } = new();

        /// <summary>
        /// 按小时分组的发送统计
        /// </summary>
        public Dictionary<int, HourlyStatistics> HourlyBreakdown { get; set; } = new();

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime LastUpdatedTime { get; set; }
    }

    /// <summary>
    /// 统计周期
    /// </summary>
    public class StatisticsPeriod
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
    }

    /// <summary>
    /// 小时统计
    /// </summary>
    public class HourlyStatistics
    {
        /// <summary>
        /// 小时（0-23）
        /// </summary>
        public int Hour { get; set; }

        /// <summary>
        /// 发送数量
        /// </summary>
        public long SentCount { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public long FailureCount { get; set; }
    }

    /// <summary>
    /// 渠道配置导出数据
    /// </summary>
    public class ChannelConfigurationExport
    {
        /// <summary>
        /// 导出时间
        /// </summary>
        public DateTime ExportTime { get; set; }

        /// <summary>
        /// 导出版本
        /// </summary>
        public string Version { get; set; } = "1.0";

        /// <summary>
        /// 导出说明
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 渠道配置列表
        /// </summary>
        public List<ChannelConfiguration> Configurations { get; set; } = new();

        /// <summary>
        /// 导出元数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();
    }

    /// <summary>
    /// 渠道配置导入结果
    /// </summary>
    public class ChannelConfigurationImportResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 导入时间
        /// </summary>
        public DateTime ImportTime { get; set; }

        /// <summary>
        /// 总配置数
        /// </summary>
        public int TotalConfigurations { get; set; }

        /// <summary>
        /// 成功导入数
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 跳过数（已存在且未覆盖）
        /// </summary>
        public int SkippedCount { get; set; }

        /// <summary>
        /// 导入结果详情
        /// </summary>
        public List<ImportResultDetail> Details { get; set; } = new();

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 导入结果详情
    /// </summary>
    public class ImportResultDetail
    {
        /// <summary>
        /// 渠道类型
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 导入状态
        /// </summary>
        public ImportStatus Status { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 导入状态
    /// </summary>
    public enum ImportStatus
    {
        /// <summary>
        /// 成功导入
        /// </summary>
        Imported,

        /// <summary>
        /// 更新现有
        /// </summary>
        Updated,

        /// <summary>
        /// 跳过（已存在）
        /// </summary>
        Skipped,

        /// <summary>
        /// 导入失败
        /// </summary>
        Failed
    }
}